package com.zhuiyun.educations.api.course.service.impl;

import com.zhuiyun.educations.api.course.entity.Course;
import com.zhuiyun.educations.api.course.mapper.CourseMapper;
import com.zhuiyun.educations.api.course.service.CourseService;
import com.zhuiyun.educations.api.courseprice.entity.Price;
import com.zhuiyun.educations.api.courseprice.mapper.CoursePriceMapper;
import com.zhuiyun.educations.api.rotationimage.entity.ImageFile;
import com.zhuiyun.educations.api.rotationimage.mapper.ImageFileMapper;
import com.zhuiyun.educations.common.errorCode.CommonException;
import com.zhuiyun.educations.common.response.CommonResult;
import com.zhuiyun.educations.util.UploadFileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CourseServiceImpl
 * @Description TODO
 * @Author 杨友朋
 * @Date 2023/6/19 14:13
 **/
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    CourseMapper courseMapper;

    @Resource
    CoursePriceMapper coursePriceMapper;

    @Resource
    ImageFileMapper imageFileMapper;

    @Resource
    UploadFileUtils uploadFileUtils;

    /**
    * @Author 杨友朋
    * @Description TODO 根据id修改
    * @Date 2023/6/26 11:34
    * @Return CommonResult
    * @param course
    */
    @Override
    public CommonResult updateCourseById(MultipartFile imageFile,Course course) {
        if(course.getId()!=null){
            if(imageFile!=null) {
                //上传至服务器
                String s = uploadFileUtils.uploadFile(imageFile);
                //判断图片id是否为空
                if (!StringUtils.isEmpty(s)) {
                    ImageFile image = new ImageFile();
                    image.setImageName(s);
                    image.setCreateTime(new Date());
                    image.setUpdateTime(new Date());
                    image.setFileSize(imageFile.getSize());
                    image.setFileUuidName(imageFile.getOriginalFilename());
                    image.setFileType(imageFile.getContentType());
                    //持久化操作
                    imageFileMapper.insertSelective(image);
                    course.setUpdateTime(new Date());
                    course.setImageId(Integer.parseInt(s));
                    courseMapper.updateCourseById(course);
                    if (course.getPayType() == 1) {
                        Price p = coursePriceMapper.selectById(course.getId());
                        if (p == null) {
                            Price price = new Price();
                            price.setId(course.getId());
                            price.setPrice(0.00);
                            price.setCreateTime(new Date());
                            price.setUpdateTime(new Date());
                            price.setDelFlag(0);
                            coursePriceMapper.insertSelective(price);
                        }
                    }
                    return CommonResult.ok("修改成功");
                }else {
                    return CommonResult.error("图片id为null");
                }
            }else {
                    course.setUpdateTime(new Date());
                    courseMapper.updateCourseById(course);
                    if(course.getPayType()==1){
                        Price p = coursePriceMapper.selectById(course.getId());
                        if(p==null){
                            Price price = new Price();
                            price.setId(course.getId());
                            price.setPrice(0.00);
                            price.setCreateTime(new Date());
                            price.setUpdateTime(new Date());
                            price.setDelFlag(0);
                            coursePriceMapper.insertSelective(price);
                        }
                    }
                    return CommonResult.ok("修改成功");
                }
        }else {
            return CommonResult.error("id不能为null");
        }
    }

    /**
    * @Author 杨友朋
    * @Description TODO 添加
    * @Date 2023/6/26 11:35
    * @Return CommonResult
    * @param course
    */
    @Override
    public CommonResult insertCourse(MultipartFile imageFile, MultipartFile file,Course course) throws CommonException {
        if(course.getId()==null){
            course.setCreateTime(new Date());
            course.setUpdateTime(new Date());
            if (imageFile!=null){
                String s=uploadFileUtils.uploadFile(imageFile);
                if(!StringUtils.isEmpty(s)) {
                    ImageFile image=new ImageFile();
                    image.setImageName(s);
                    image.setCreateTime(new Date());
                    image.setUpdateTime(new Date());
                    image.setFileSize(imageFile.getSize());
                    image.setFileUuidName(imageFile.getOriginalFilename());
                    image.setFileType(imageFile.getContentType());
                    //持久化操作;
                    imageFileMapper.insertSelective(image);
                    course.setImageId(Integer.parseInt(s));
                }
            }
//            String projectName=new String();
//            FileManage fileManage1 = fileManageService.saveFileToSystem(infoFile, projectName);
//            course.setInformationId(fileManage1.getId());
            int i = courseMapper.insertCourse(course);
            Course c = courseMapper.selectById(course.getId());

            if(c.getPayType()==1) {
                Price price = new Price();
                price.setId(c.getId());
                price.setPrice(0.00);
                price.setCreateTime(new Date());
                price.setUpdateTime(new Date());
                price.setDelFlag(0);
                coursePriceMapper.insertSelective(price);
            }
            return CommonResult.ok();
        }
        else {
            return CommonResult.error("添加失败");
        }
    }

    /**
    * @Author 杨友朋
    * @Description TODO 根据id删除
    * @Date 2023/6/26 11:38
    * @Return CommonResult
    * @param id
    */
    @Override
    public CommonResult deleteCourseById(Integer id) {
        if(id!=null){
            courseMapper.deleteCourseById(id);
            return CommonResult.ok("删除成功");

        }else {
            return CommonResult.error("删除失败");

        }
    }

    /**
    * @Author 杨友朋
    * @Description TODO 根据ids批量删除
    * @Date 2023/6/26 11:42
    * @Return CommonResult
    * @param ids
    */
    @Override
    public CommonResult deleteByCourseIds(List<Integer> ids) {
        if(ids!=null){
            courseMapper.deleteCourseByIds(ids);
            return CommonResult.ok("批量删除成功");
        }else {
            return CommonResult.error("批量删除失败");
        }

    }

    /**
    * @Author 杨友朋
    * @Description 根据id修改状态
    * @Date 2023/6/26 11:47
    * @Return CommonResult
    * @param course
    */
    @Override
    public CommonResult updateCourseStatusById(Course course) {
        if(course.getId()!=null){
            courseMapper.updateStatusById(course);
            return CommonResult.ok("修改状态成功");
        }else {
            return CommonResult.error("修改状态失败");
        }

    }

    @Override
    public CommonResult updateCourseByIds(List<Integer> ids, Course course) {
        return null;
    }
}
